// pages/product/product.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
var start = 0;

Page(Object.assign({}, Zan.Tab, Zan.TopTips, {
  /**
   * 页面的初始数据
   */
  data: {
    tab: {},
    menu: [],
    title: '',
    // order: [{
    //   id: 0,
    //   order: 'Asc',
    //   type: 'orderNo',
    //   name: '默认排序'
    // }, {
    //   id: 1,
    //   order: 'Desc',
    //   type: 'totalSalesBaseQuantity',
    //   //type: 'totalSales',
    //   name: '销量'
    // }, {
    //   id: 2,
    //   order: 'Asc',
    //   type: 'price',
    //   name: '价格'
    // }, {
    //   id: 3,
    //   order: 'Desc',
    //   type: 'createDate',
    //   name: '新品上市'
    // }],
    product: [],
    recordsTotal: 0,
    typeId: null,
    orderNO: 0,
    radioCheckVal: 0,
    radioCheckOrderVal: 0,
    selectedId: 0,
    showCenterBtn: true,
    nodata: {
      msg: '该分类下没有商品',
      notype: 2,
      imgurl: '../../images/nodatas.png',
      nourl: ''
    },
    nodata_s: {
      msg: '该商品不存在',
      notype: 2,
      imgurl: '../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      menu: [],
      typeId: null
    })

    let linkId = wx.getStorageSync('linkId')
    var menu = {};
    menu.id = null;
    menu.name = '所有';
    menu.orderNo = 0;
    this.setData({
      menu: this.data.menu.concat(menu),
      product: [],
      typeId: linkId
    })

    network._post_json(this, api.getProductGroup, {}, successType);//Asc
    //network._post_page_json(this, api.getProductList, start, { typeId: this.data.typeId }, success);
    if (this.data.typeId) {
      network._post_product_request(this, api.getProductList, start, { typeId: this.data.typeId }, success);
    } else {
      network._post_product_request(this, api.getProductList, start, {}, success);
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // this.setData({
    //   menu: [],
    //   product: [],
    //   typeId: null
    // })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      product: []
    });

    network._post_product_request(this, api.getProductList, start, { typeId: this.data.menu[this.data.selectedId].id }, success);

    // var menu = {};
    // menu.id = null;
    // menu.name = '所有';
    // menu.orderNo = 0;
    // var m = [menu];
    // this.setData({
    //   menu: m,
    // })

    // network._post_json(this, api.getProductGroup, {}, successType);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.product.length < this.data.recordsTotal) {
      start = start + 20;
      network._post_product_request(this, api.getProductList, start, { typeId: this.data.menu[this.data.selectedId].id }, success);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  /**
    * 进入搜索页面
    */
  cancel: function () {
    this.setData({
      title: ''
    })
  },

  /**
   * 选择类型
   */
  radioCheckedChange: function (e) {
    this.setData({
      radioCheckVal: e.detail.value,
      typeId: this.data.menu[e.detail.value].id,
      activeIndex: 10
    })
    this.onPullDownRefresh();
  },

  /**
  * 选择排序
  */
  checkedChangeOrder: function (e) {
    this.setData({
      radioCheckOrderVal: e.detail.value,
      orderNO: e.detail.value,
      activeIndex: 10
    })
    this.onPullDownRefresh();
  },

  /**
   * 点击商品进入商品详情
   */
  onProductItemClick: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/product/ProductDetails' + '?productId=' + id
    })
  },

  /**
  * 
  */
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      product: [],
      selectedId: selectedId,
      [`${componentId}.selectedId`]: selectedId
    });

    start = 0;
    network._post_product_request(this, api.getProductList, start, { typeId: this.data.menu[selectedId].id }, success);
  },

  search: function (e) {
    start = 0;
    wx.stopPullDownRefresh();
    this.setData({
      product: [],
    });
    network._post_product_request(this, api.getProductList, start, { title: e.detail.value }, success);
  },

  //进入购物车
  go_shopcar: function (e) {
    wx.navigateTo({
      url: '/pages/shoppingCar/ShoppingCar',
    })
  },

  //进入搜索页面
  goProductSearch: function (e) {
    wx.navigateTo({
      url: '/pages/product/ProductSearch',
    })
  }
}))

// 获取商品分类结果
var successType = function (that, res) {
  if (res.data.data) {
    that.setData({
      menu: that.data.menu.concat(res.data.data)
    })

    var menuItems = []
    for (var i = 0; i < that.data.menu.length; i++) {
      var menuItem = {};
      menuItem.id = i;
      menuItem.title = that.data.menu[i].name;
      menuItems.push(menuItem);
    }

    var index = that.data.selectedId;
    for (var i = 0; i < that.data.menu.length; i++) {
      if (that.data.menu[i].id == that.data.typeId) {
        index = i;
      }
    }

    that.setData({
      tab: {
        list: menuItems,
        scroll: false,
        selectedId: index
      }
    })
  }
}

// 获取商品列表结果
var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      product: that.data.product.concat(res.data.data),
      recordsTotal: res.data.recordsTotal
    })
  }
}

var menuHeight = function (that, res) {
  var numsfenlei = res.length + 1;
  var listheght = 120;
  if (numsfenlei <= 3) {
    listheght = 120;
  } else {
    if (numsfenlei % 3 == 1) {
      listheght = (numsfenlei + 2) / 3 * 100
    } else if (numsfenlei % 3 == 2) {
      listheght = (numsfenlei + 1) / 3 * 100
    } else if (numsfenlei % 3 == 0) {
      listheght = numsfenlei / 3 * 100
    }
  }
  return listheght;
}